<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/12
 * Time: 14:23
 */

namespace zhtfrontend\components\services\goods;

use common\components\helpers\Fun;
use common\models\datas\Brand;
use common\models\datas\Cate;
use yii\base\Action;
use yii\web\BadRequestHttpException;
use zhtfrontend\components\helpers\Funs;
use zhtfrontend\models\datas\Goods;
use yii\web\Response;
use yii\widgets\ActiveForm;
use yii\db\Query;
use common\components\helpers\Util;
use zhtfrontend\models\datas\GoodsClassTime;
use zhtfrontend\models\datas\GoodsPrice;
use zhtfrontend\models\datas\Mechanic;

class AuditingAction extends Action
{
    public $default_url = '@zhtfrontend/components/services/goods/CommondetailView.php';
    public $type;

    public function run()
    {
        $site_id = Fun::getSiteId();
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('frontend', '商品id为空'));
        }
        $goods_id = \Yii::$app->request->get('id');
        $model = Goods::findOne([$goods_id]);
        $model->scenario = 'auditing';
        $price = GoodsPrice::find()->where(['goods_id'=>$goods_id,'status'=>1])->all();
        if($price == null || $model->deposit <= 0){
            throw new BadRequestHttpException(\Yii::t('frontend', '请先设置保证金与价格套餐'));
        }
//        $model->scenario = 'auditing';
        if (\Yii::$app->request->get('type_id') == null) {
            $this->type = $model->type_id;
        } else {
            $this->type = \Yii::$app->request->get('type_id');
        }
        $category = null;
        $brand = null;
        $mechanic = null;
        if ($model->cat_id != null) {//分类id不为空
            $category = Cate::findOne($model->cat_id);
        }
        if ($model->brand_id != null) {
            $brand = Brand::findOne($model->brand_id);
        }
        if ($model->mechanic_id != null) {
            $mechanic = Mechanic::findOne(['id' => $model->mechanic_id, 'site_id' => $site_id]);
        }
        //获取课程或舞台类型特有的数据们
        $specialdatas = $this->getSpecialModuleDatas($this->type, $goods_id);
        //获取商品扩展属性显示
        $attributedetail = Funs::getExtendattributeDetails($this->type, $goods_id);
        $goodsprice = GoodsPrice::find()->where(['goods_id' => $goods_id])->orderBy('id DESC')->all();
        $goodsprice = Fun::asArray($goodsprice);
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            if($model->status == 1){
                $model->is_shelve = 1;
            }else{
                $model->is_shelve = 0;
            }
            $model->updated_at = time();
            if ($model->save(false)) {
                $url = Fun::url([$this->controller->id.'/index','type_id'=>$this->type]);
                return $this->controller->redirectMessage('操作成功',$url);
            }
        }
        return $this->controller->render('auditing', ['model' => $model, 'goodsprice' => $goodsprice, 'specialdatas' => $specialdatas, 'attributedetail' => $attributedetail, 'default' => \Yii::$app->view->render($this->default_url, ['model' => $model, 'category' => $category, 'brand' => $brand, 'mechanic' => $mechanic, 'attributedetail' => $attributedetail])]);

    }

    //获取某种类型商品特有的扩展数据
    public function getSpecialModuleDatas($typeid, $goodid)
    {
        if ($goodid == null || $typeid == null) {
            return null;
        }
        $good = Goods::findOne($goodid);
        if (empty($good)) {
            return null;
        }
        if ($typeid == 1) {//课程产品
            //$arrange = Arrangement::findOne($good->arrangement_id)->toArray([],['course_name','teacher_name','room_number','class_day','start_time','end_time']);//根据排课id获取排课记录
            $goodsarr_query = (new Query())->select(["goods_id", "max(IF(name='课程',value,'')) as course_name", "max(IF(name='老师',value,'')) as teacher_name", "max(IF(name='教室编号',value,'')) as room_number", "max(IF(name='教室名称',value,'')) as room_name", "max(IF(name='开课人数',value,'')) as opening_number", "max(IF(name='最满人数',value,'')) as max_number",
                "max(IF(name='是否可预约',value,'')) as is_booking", "max(IF(name='上课次数',value,'')) as class_count"])->from('{{%goods_attr}}')->groupBy('goods_id')->having(['goods_id' => $good->id])->one(Fun::getSiteDb());
            //$array = Fun::asArray($arrange,['course_name','teacher_name','room_number','class_day','start_time','end_time']);
            $str = '';
            $times = GoodsClassTime::find()->where(['goods_id' => $goodid])->asArray()->all();
            if ($times) {
                foreach ($times as $t) {
                    $str .= Util::getWeek($t['class_day']) . $t['start_time'] . '-' . $t['end_time'];
                }
            }
            $goodsarr_query['class_day'] = $str;
            return $goodsarr_query;
        } else {
            //实现其他类型商品的特殊数据
            return null;
        }
    }
}

?>